package com.lanmao.mall.util;

import com.lanmao.mall.enums.StatusCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 描述：
 *
 * @author lanmao
 * @date 2023年11月24日 12:07
 */
@Data
public class ResultResponse<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public static <T> ResultResponse<T> success(T data) {
        return buildResultResponse(StatusCode.SUCCESS, data);
    }
    public static <T> ResultResponse<T> success(T data, String message) {
        return buildResultResponse(StatusCode.SUCCESS.getCode(), message, data);
    }
    public static <T> ResultResponse<T> error(StatusCode statusCode) {
        return buildResultResponse(statusCode, null);
    }
    public static <T> ResultResponse<T> error() {
        return buildResultResponse(StatusCode.ERROR, null);
    }

    private static <T> ResultResponse<T> buildResultResponse(Integer code, String message, T data) {
        ResultResponse<T> resultRespose = new ResultResponse<T>();
        resultRespose.setData(data);
        resultRespose.setCode(code);
        resultRespose.setMessage(message);
        return resultRespose;
    }
    private static <T> ResultResponse<T> buildResultResponse(StatusCode statusCode, T data) {
        ResultResponse<T> resultRespose = new ResultResponse<T>();
        resultRespose.setData(data);
        resultRespose.setCode(statusCode.getCode());
        resultRespose.setMessage(statusCode.getMessage());
        return resultRespose;
    }
}
